﻿#region License
/*Copyright (c) 2010 Andrew Collard (andrewcollard@hotmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#endregion

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace TorchView.Windows
{
  /// <summary>
  /// Interaction logic for MessageBox.xaml
  /// </summary>
  public partial class MessageBox : Window
  {
    private WindowService _WindowService;

    private MessageBoxButton _Buttons;
    public MessageBoxButton Buttons
    {
      get { return _Buttons; }
      set { UpdateButtons(value); }
    }

    private MessageBoxImage _Image;
    public MessageBoxImage Image
    {
      get { return _Image; }
      set { AssignImage(value); }
    }

    public MessageBoxResult Result { get; set; }

    public string Message
    {
      get { return MessageBlock.Text; }
      set { MessageBlock.Text = value; }
    }

    public MessageBox()
    {
      InitializeComponent();
      _WindowService = new WindowService(this);
      DataContext = this;
      CancelCommand = new DirectCommand(p => Cancel());
    }

    public ICommand CancelCommand { get; private set; }

    private void AssignImage(MessageBoxImage value)
    {
      switch (value)
      {
        case MessageBoxImage.Error:
          MessageImage.Source = new BitmapImage(new Uri("/TorchView.Common;Component/Images/error.png", UriKind.RelativeOrAbsolute));
          break;
        case MessageBoxImage.Exclamation:
          MessageImage.Source = new BitmapImage(new Uri("/TorchView.Common;Component/Images/warning.png", UriKind.RelativeOrAbsolute));
          break;
        case MessageBoxImage.Information:
          MessageImage.Source = new BitmapImage(new Uri("/TorchView.Common;Component/Images/info.png", UriKind.RelativeOrAbsolute));
          break;
        case MessageBoxImage.Question:
          MessageImage.Source = new BitmapImage(new Uri("/TorchView.Common;Component/Images/help.png", UriKind.RelativeOrAbsolute));
          break;
        case MessageBoxImage.None:
        default:
          MessageImage.Source = null;
          break;
      }
    }

    private void UpdateButtons(MessageBoxButton value)
    {
      _Buttons = value;
      switch (value)
      {
        case MessageBoxButton.OKCancel:
          YesButton.Visibility = Visibility.Visible;
          CancelButton.Visibility = Visibility.Visible;
          NoButton.Visibility = Visibility.Collapsed;
          YesButton.Content = "OK";
          CancelButton.Content = "Cancel";
          if (Result == MessageBoxResult.OK)
            YesButton.IsDefault = true;
          else if (Result == MessageBoxResult.Cancel)
            CancelButton.IsDefault = true;
          break;
        case MessageBoxButton.YesNo:
          YesButton.Visibility = Visibility.Visible;
          NoButton.Visibility = Visibility.Visible;
          CancelButton.Visibility = Visibility.Collapsed;
          YesButton.Content = "Yes";
          NoButton.Content = "No";
          if (Result == MessageBoxResult.Yes)
            YesButton.IsDefault = true;
          else if (Result == MessageBoxResult.No)
            NoButton.IsDefault = true;
          break;
        case MessageBoxButton.YesNoCancel:
          YesButton.Visibility = Visibility.Visible;
          NoButton.Visibility = Visibility.Visible;
          CancelButton.Visibility = Visibility.Visible;
          YesButton.Content = "Yes";
          NoButton.Content = "No";
          CancelButton.Content = "Cancel";
          if (Result == MessageBoxResult.Yes)
            YesButton.IsDefault = true;
          else if (Result == MessageBoxResult.No)
            NoButton.IsDefault = true;
          else if (Result == MessageBoxResult.Cancel)
            CancelButton.IsDefault = true;
          break;
        case MessageBoxButton.OK:
        default:
          YesButton.Visibility = Visibility.Visible;
          NoButton.Visibility = Visibility.Collapsed;
          CancelButton.Visibility = Visibility.Collapsed;
          YesButton.Content = "OK";
          if (Result == MessageBoxResult.OK)
            YesButton.IsDefault = true;
          break;
      }
    }

    private void YesButton_Click(object sender, RoutedEventArgs e)
    {
      Yes();
    }

    private void Yes()
    {
      switch (Buttons)
      {
        case MessageBoxButton.OK:
        case MessageBoxButton.OKCancel:
          Result = MessageBoxResult.OK;
          break;
        case MessageBoxButton.YesNo:
        case MessageBoxButton.YesNoCancel:
          Result = MessageBoxResult.Yes;
          break;
        default:
          Result = MessageBoxResult.None;
          break;
      }
      Close();
    }

    private void NoButton_Click(object sender, RoutedEventArgs e)
    {
      No();
    }

    private void No()
    {
      switch (Buttons)
      {
        case MessageBoxButton.YesNo:
        case MessageBoxButton.YesNoCancel:
          Result = MessageBoxResult.No;
          break;
        case MessageBoxButton.OK:
        case MessageBoxButton.OKCancel:
        default:
          Result = MessageBoxResult.None;
          break;
      }
      Close();
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
      Cancel();
    }

    private void Cancel()
    {
      switch (Buttons)
      {
        case MessageBoxButton.OKCancel:
        case MessageBoxButton.YesNoCancel:
          Result = MessageBoxResult.Cancel;
          break;
        case MessageBoxButton.YesNo:
        case MessageBoxButton.OK:
        default:
          Result = MessageBoxResult.None;
          break;
      }
      Close();
    }
  }
}
